import 'dart:convert';

/// 所有钱包集合
class WalletListModel {
  List<WalletBean> walletList;

  static WalletListModel fromMap(Map<String, dynamic> map) {
    if (map == null) return null;
    WalletListModel walletListModelBean = WalletListModel();
    walletListModelBean.walletList = List()
      ..addAll((map['wallet_list'] as List ?? []).map((o) => WalletBean.fromMap(o)));
    return walletListModelBean;
  }

  Map toJson() => {
        "wallet_list": walletList.map((walletBean) {
          return walletBean.toJson();
        }).toList(),
      };
}

/// 单个钱包
class WalletBean {
  String walletName;
  WalletCoinListModel coins;
  String verifiedMnemonic;
  String mnemonicString;
  WalletTransferRecordListModel walletTransferRecordListModel;
  WalletDepositRecordModel walletDepositRecordModel;
  WalletInterestListModel walletInterestListModel;

  static WalletBean fromMap(Map<String, dynamic> map) {
    if (map == null) return null;
    WalletBean walletBean = WalletBean();
    walletBean.walletName = map['wallet_name'];
    walletBean.coins = WalletCoinListModel.fromMap(json.decode(map['all_coin_list']));
    walletBean.verifiedMnemonic = map['verified_mnemonic'];
    walletBean.mnemonicString = map['mnemonic_string'];
    walletBean.walletTransferRecordListModel =
        WalletTransferRecordListModel.fromMap(json.decode(map['transfer_record_key']));
    walletBean.walletDepositRecordModel =
        WalletDepositRecordModel.fromMap(json.decode(map['deposit_purchase_record_key']));
    walletBean.walletInterestListModel = WalletInterestListModel.fromMap(json.decode(map['defi_interest_list_key']));
    return walletBean;
  }

  Map toJson() => {
        "wallet_name": walletName,
        "all_coin_list": coins.coins.map((e) => e.toJson()).toList(),
        "verified_mnemonic": verifiedMnemonic,
        "mnemonic_string": mnemonicString,
        "transfer_record_key": walletTransferRecordListModel.transferRecords.map((e) => e.toJson()).toList(),
        "deposit_record_key": walletDepositRecordModel.depositRecords.map((e) => e.toJson()).toList(),
        "defi_interest_list_key": walletInterestListModel.interestList.map((e) => e.toJson()).toList(),
      };
}

class WalletInterestListModel {
  List<WalletInterestModel> interestList;

  static WalletInterestListModel fromMap(Map<String, dynamic> map) {
    if (map == null) return null;
    WalletInterestListModel walletInterestListModel = WalletInterestListModel();
    walletInterestListModel.interestList = List()
      ..addAll((json.decode(map['defi_interest_list_key'].toString()) as List ?? [])
          .map((o) => WalletInterestModel.fromMap(o)));
    return walletInterestListModel;
  }

  Map toJson() => {
        "defi_interest_list_key": interestList.map((e) => e.toJson()).toList(),
      };
}

class WalletInterestModel {
  String time;
  String interest;

  static WalletInterestModel fromMap(Map<String, dynamic> map) {
    if (map == null) return null;
    WalletInterestModel walletInterestModel = WalletInterestModel();
    walletInterestModel.time = map['time'];
    walletInterestModel.interest = map['interest'];
    return walletInterestModel;
  }

  Map toJson() => {"time": time, "interest": interest};
}

/// coins : [{"img":"assets/images/btc_icon.png","title":"BTC","coinid":"BTC","cny_balance":"84412.72","num":"0.00000000","cny_money":"0.00"},{"img":"assets/images/eth_icon.png","title":"ETH","coinid":"ETH","cny_balance":"2582.06","num":"0.00000000","cny_money":"0.00"},{"img":"assets/images/usdt_icon.png","title":"USDT","coinid":"USDT","cny_balance":"0.00","num":"0.00000000","cny_money":"0.00"},{"img":"assets/images/ht_icon.png","title":"HT","coinid":"HT","cny_balance":"29.84","num":"0.00000000","cny_money":"0.00"},{"img":"assets/images/delc_icon.png","title":"DELC","coinid":"DELC","cny_balance":"0.00","num":"0.00000000","cny_money":"0.00"},{"img":"assets/images/ae_icon.png","title":"AE","coinid":"AE","cny_balance":"0.74","num":"0.00000000","cny_money":"0.00"},{"img":"assets/images/bat_icon.png","title":"BAT","coinid":"BAT","cny_balance":"1.44","num":"0.00000000","cny_money":"0.00"},{"img":"assets/images/elf_icon.png","title":"ELF","coinid":"ELF","cny_balance":"0.62","num":"0.00000000","cny_money":"0.00"},{"img":"assets/images/omg_icon.png","title":"OMG","coinid":"OMG","cny_balance":"22.17","num":"0.00000000","cny_money":"0.00"},{"img":"assets/images/zrx_icon.png","title":"ZRX","coinid":"ZRX","cny_balance":"2.51","num":"0.00000000","cny_money":"0.00"},{"img":"assets/images/gusd_icon.png","title":"GUSD","coinid":"GUSD","cny_balance":"6.67","num":"0.00000000","cny_money":"0.00"},{"img":"assets/images/lrc_icon.png","title":"LRC","coinid":"LRC","cny_balance":"1.12","num":"0.00000000","cny_money":"0.00"},{"img":"assets/images/ggc_icon.png","title":"GGC","coinid":"GGC","cny_balance":"4.10","num":"0.00000000","cny_money":"0.00"},{"img":"assets/images/ggt_icon.png","title":"GGT","coinid":"GGT","cny_balance":"0.00","num":"0.00000000","cny_money":"0.00"}]
/// 单个钱包币种信息集合
class WalletCoinListModel {
  List<CoinsBean> coins;

  static WalletCoinListModel fromMap(Map<String, dynamic> map) {
    if (map == null) return null;
    WalletCoinListModel walletListModelBean = WalletCoinListModel();
    walletListModelBean.coins = List()
      ..addAll((json.decode(map['all_coin_list'].toString()) as List ?? []).map((o) => CoinsBean.fromMap(o)));
    return walletListModelBean;
  }

  Map toJson() => {
        "all_coin_list": coins.map((e) => e.toJson()).toList(),
      };
}

/// img : "assets/images/btc_icon.png"
/// title : "BTC"
/// coinid : "BTC"
/// cny_balance : "84412.72"
/// num : "0.00000000"
/// cny_money : "0.00"
/// time: "2020.10.26 16:00"
/// address: "annfghd34a3dasasdassfdgfdgh2fsd4fsdf1sd2f1sd2"
/// upDown: "0.02345"
/// 单个钱包单个币种
class CoinsBean {
  String img;
  String title;
  String coinid;
  String cnyBalance;
  String num;
  String cnyMoney;
  String time;
  String address;
  String upDown;
  String privateKey;
  String publicKey;

  static CoinsBean fromMap(Map<String, dynamic> map) {
    if (map == null) return null;
    CoinsBean coinsBean = CoinsBean();
    coinsBean.img = map['img'];
    coinsBean.title = map['title'];
    coinsBean.coinid = map['coinid'];
    coinsBean.cnyBalance = map['cny_balance'];
    coinsBean.num = map['num'];
    coinsBean.cnyMoney = map['cny_money'];
    coinsBean.isSelected = map["isSelected"];
    coinsBean.time = map['time'];
    coinsBean.address = map['address'];
    coinsBean.upDown = map['upDown'];
    coinsBean.privateKey = map['privateKey'];
    coinsBean.publicKey = map['publicKey'];
    return coinsBean;
  }

  bool isSelected;

  Map toJson() => {
        "img": img,
        "title": title,
        "coinid": coinid,
        "cny_balance": cnyBalance,
        "num": num,
        "cny_money": cnyMoney,
        "time": time,
        "address": address,
        "upDown": upDown,
        "privateKey": privateKey,
        "publicKey": publicKey,
        "isSelected": isSelected
      };

  static List<CoinsBean> toList(List list) {
    if (List == null || list.length == 0) return [];
    List<CoinsBean> listBean = List();
    list.forEach((data) {
      CoinsBean bean = CoinsBean();
      bean.img = data['img'];
      bean.title = data['title'];
      bean.coinid = data['coinid'];
      bean.cnyBalance = data['cny_balance'];
      bean.num = data['num'];
      bean.cnyMoney = data['cny_money'];
      bean.isSelected = data['isSelected'];
      bean.address = data['address'];
      bean.upDown = data['upDown'];
      bean.time = data['time'];
      bean.privateKey = data['privateKey'];
      bean.publicKey = data['publicKey'];
      bean.isSelected = data['isSelected'];
      listBean.add(bean);
    });
    return listBean;
  }
}

/// 转账记录集合
class WalletTransferRecordListModel {
  List<CoinDetailModel> transferRecords;

  static WalletTransferRecordListModel fromMap(Map<String, dynamic> map) {
    if (map == null) return null;
    WalletTransferRecordListModel walletTransferRecordListModel = WalletTransferRecordListModel();
    walletTransferRecordListModel.transferRecords = List()
      ..addAll(
          (json.decode(map['transfer_record_key'].toString()) as List ?? []).map((o) => CoinDetailModel.fromMap(o)));
    return walletTransferRecordListModel;
  }

  Map toJson() => {
        "transfer_record_key": transferRecords.map((e) => e.toJson()).toList(),
      };
}

class CoinDetailModel {
  String img;
  String name;
  String time;
  String upDown;
  String cnyMoney;
  String address;
  String transferType;

  static CoinDetailModel fromMap(Map<String, dynamic> map) {
    if (map == null) return null;
    CoinDetailModel coinDetailModel = CoinDetailModel();
    coinDetailModel.img = map['img'];
    coinDetailModel.name = map['name'];
    coinDetailModel.time = map['time'];
    coinDetailModel.upDown = map['upDown'];
    coinDetailModel.cnyMoney = map['cnyMoney'];
    coinDetailModel.address = map['address'];
    coinDetailModel.transferType = map['transferType'];
    return coinDetailModel;
  }

  static List<CoinDetailModel> fromList(List list) {
    if (list == null) return null;
    List<CoinDetailModel> coinList = List();
    list.forEach((element) {
      CoinDetailModel model = CoinDetailModel();
      model.img = element["img"];
      model.name = element["name"];
      model.time = element["time"];
      model.upDown = element["upDown"];
      model.cnyMoney = element["cnyMoney"];
      model.address = element["address"];
      model.transferType = element["transferType"];
      coinList.add(model);
    });
    return coinList;
  }

  Map toJson() => {
        "img": img,
        "name": name,
        "time": time,
        "upDown": upDown,
        "cnyMoney": cnyMoney,
        "cny_money": cnyMoney,
        "address": address,
        "transferType": transferType,
      };
}

/// 买入/赎回订单记录
class WalletDepositRecordModel {
  List<DepositRedeemModel> depositRecords;

  static WalletDepositRecordModel fromMap(Map<String, dynamic> map) {
    if (map == null) return null;
    WalletDepositRecordModel walletDepositRecordListModel = WalletDepositRecordModel();
    walletDepositRecordListModel.depositRecords = List()
      ..addAll(
          (json.decode(map['deposit_record_key'].toString()) as List ?? []).map((o) => DepositRedeemModel.fromMap(o)));
    return walletDepositRecordListModel;
  }

  Map toJson() => {
        "deposit_record_key": depositRecords.map((e) => e.toJson()).toList(),
      };
}

class DepositRedeemModel {
  /// 0 为申购，1 为赎回
  String type;
  String contractAddress;
  String from;
  String to;
  double serviceCharge;
  String time;
  String payAddress;
  String interestRate;
  String block;
  String status;

  static DepositRedeemModel fromMap(Map<String, dynamic> map) {
    if (map == null) return null;
    DepositRedeemModel model = DepositRedeemModel();
    model.type = map["type"];
    model.contractAddress = map["contract_address"];
    model.from = map["from"];
    model.to = map["to"];
    model.serviceCharge = map["service_charge"];
    model.time = map["time"];
    model.payAddress = map["pay_address"];
    model.interestRate = map["interest_rate"];
    model.block = map["block"];
    model.status = map["status"];
    return model;
  }

  static List<DepositRedeemModel> fromList(List list) {
    if (list == null) return null;
    List<DepositRedeemModel> recordList = List();
    list.forEach((map) {
      DepositRedeemModel model = DepositRedeemModel();
      model.type = map["type"];
      model.contractAddress = map["contract_address"];
      model.from = map["from"];
      model.to = map["to"];
      model.serviceCharge = map["service_charge"];
      model.time = map["time"];
      model.payAddress = map["pay_address"];
      model.interestRate = map["interest_rate"];
      model.block = map["block"];
      model.status = map["status"];
      recordList.add(model);
    });
    return recordList;
  }

  Map toJson() => {
        "type": type,
        "contract_address": contractAddress,
        "from": from,
        "to": to,
        "service_charge": serviceCharge,
        "time": time,
        "pay_address": payAddress,
        "interest_rate": interestRate,
        "block": block,
        "status": status,
      };
}

///// 赎回订单记录
//class WalletDepositRedeemRecordModel {
//  List<DepositRedeemModel> depositRedeemRecords;
//
//  static WalletDepositRedeemRecordModel fromMap(Map<String, dynamic> map) {
//    if (map == null) return null;
//    WalletDepositRedeemRecordModel walletDepositRedeemRecordListModel = WalletDepositRedeemRecordModel();
//    walletDepositRedeemRecordListModel.depositRedeemRecords = List()
//      ..addAll((json.decode(map['deposit_redeem_record_key'].toString()) as List ?? [])
//                   .map((o) => DepositRedeemModel.fromMap(o)));
//    return walletDepositRedeemRecordListModel;
//  }
//
//  Map toJson() => {
//    "deposit_redeem_record_key": depositRedeemRecords.map((e) => e.toJson()).toList(),
//  };
//}
//
//class DepositRedeemModel {
//  String contractAddress;
//  String fromCToken;
//  String toCoins;
//  String payeeTime;
//  String payeeAddress;
//  String redeemInterestRate;
//  String redeemStatus;
//
//  static DepositRedeemModel fromMap(Map<String, dynamic> map) {
//    if (map == null) return null;
//    DepositRedeemModel model = DepositRedeemModel();
//    model.contractAddress = map["contract_address"];
//    model.fromCToken = map["from_cToken"];
//    model.toCoins = map["to_coins"];
//    model.payeeTime = map["payee_time"];
//    model.payeeAddress = map["payee_address"];
//    model.redeemInterestRate = map["redeem_interest_rate"];
//    model.redeemStatus = map["redeem_status"];
//    return model;
//  }
//
//  static List<DepositRedeemModel> fromList(List list) {
//    if (list == null) return null;
//    List<DepositRedeemModel> redeemRecordList = List();
//    list.forEach((map) {
//      DepositRedeemModel model = DepositRedeemModel();
//      model.contractAddress = map["contract_address"];
//      model.fromCToken = map["from_cToken"];
//      model.toCoins = map["to_coins"];
//      model.payeeTime = map["payee_time"];
//      model.payeeAddress = map["payee_address"];
//      model.redeemInterestRate = map["redeem_interest_rate"];
//      model.redeemStatus = map["redeem_status"];
//      redeemRecordList.add(model);
//    });
//    return redeemRecordList;
//  }
//
//  Map toJson() => {
//    "contract_address": contractAddress,
//    "from_cToken": fromCToken,
//    "to_coins": toCoins,
//    "payee_time": payeeTime,
//    "payee_address": payeeAddress,
//    "redeem_interest_rate": redeemInterestRate,
//    "redeem_status": redeemStatus,
//  };
//}
